﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace MVC4WPF.Automation.Dialog
{
    /// <summary>
    /// Value Converter class that will accept a boolean and return Visible if true, or Collapsed if flase.
    /// </summary>
    [ValueConversion(typeof(Boolean), typeof(System.Windows.Visibility))]
    public class BooleanToVisibilityValueConverter:IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility returnValue = Visibility.Visible;
             
            try
            {
                bool visibilityFlag = (bool)value;
                if (!visibilityFlag)
                {
                    returnValue = Visibility.Collapsed;
                }

            }
            catch (System.Exception)
            {
                return DependencyProperty.UnsetValue;
            }
            return returnValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool returnValue = true;

            try
            {
                Visibility visibilityFlag = (Visibility)value;
                if (visibilityFlag == Visibility.Collapsed | visibilityFlag == Visibility.Hidden)
                {
                    returnValue = false;
                }

            }
            catch (System.Exception)
            {
                return DependencyProperty.UnsetValue;
            }

            return returnValue;
        }

        #endregion
    }
}
